{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Browserbase\n",
    "\n",
    "[Browserbase](https://browserbase.com) is a developer platform to reliably run, manage, and monitor headless browsers.\n",
    "\n",
    "Power your AI data retrievals with:\n",
    "- [Serverless Infrastructure](https://docs.browserbase.com/under-the-hood) providing reliable browsers to extract data from complex UIs\n",
    "- [Stealth Mode](https://docs.browserbase.com/features/stealth-mode) with included fingerprinting tactics and automatic captcha solving\n",
    "- [Session Debugger](https://docs.browserbase.com/features/sessions) to inspect your Browser Session with networks timeline and logs\n",
    "- [Live Debug](https://docs.browserbase.com/guides/session-debug-connection/browser-remote-control) to quickly debug your automation\n",
    "\n",
    "## Installation and Setup\n",
    "\n",
    "- Get an API key and Project ID from [browserbase.com](https://browserbase.com) and set it in environment variables (`BROWSERBASE_API_KEY`, `BROWSERBASE_PROJECT_ID`).\n",
    "- Install the [Browserbase SDK](http://github.com/browserbase/python-sdk):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "% pip install browserbase"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loading documents"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can load webpages into LangChain using `BrowserbaseLoader`. Optionally, you can set `text_content` parameter to convert the pages to text-only representation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.document_loaders import BrowserbaseLoader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "loader = BrowserbaseLoader(\n",
    "    urls=[\n",
    "        \"https://example.com\",\n",
    "    ],\n",
    "    # Text mode\n",
    "    text_content=False,\n",
    ")\n",
    "\n",
    "docs = loader.load()\n",
    "print(docs[0].page_content[:61])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Loader Options\n",
    "\n",
    "- `urls` Required. A list of URLs to fetch.\n",
    "- `text_content` Retrieve only text content. Default is `False`.\n",
    "- `api_key` Optional. Browserbase API key. Default is `BROWSERBASE_API_KEY` env variable.\n",
    "- `project_id` Optional. Browserbase Project ID. Default is `BROWSERBASE_PROJECT_ID` env variable.\n",
    "- `session_id` Optional. Provide an existing Session ID.\n",
    "- `proxy` Optional. Enable/Disable Proxies."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loading images\n",
    "\n",
    "You can also load screenshots of webpages (as bytes) for multi-modal models.\n",
    "\n",
    "Full example using GPT-4V:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from browserbase import Browserbase\n",
    "from browserbase.helpers.gpt4 import GPT4VImage, GPT4VImageDetail\n",
    "from langchain_core.messages import HumanMessage\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "chat = ChatOpenAI(model=\"gpt-4-vision-preview\", max_tokens=256)\n",
    "browser = Browserbase()\n",
    "\n",
    "screenshot = browser.screenshot(\"https://browserbase.com\")\n",
    "\n",
    "result = chat.invoke(\n",
    "    [\n",
    "        HumanMessage(\n",
    "            content=[\n",
    "                {\"type\": \"text\", \"text\": \"What color is the logo?\"},\n",
    "                GPT4VImage(screenshot, GPT4VImageDetail.auto),\n",
    "            ]\n",
    "        )\n",
    "    ]\n",
    ")\n",
    "\n",
    "print(result.content)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
